﻿using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Data;
using System.Windows.Input;
using JulMar.Windows.Behaviors;
using JulMar.Windows.Mvvm;
using MediaBrowser.Entities;
using MediaBrowser.Gui.MvvmUtils.UI;
using MediaBrowser.IServices;

namespace MediaBrowser.Gui.ViewModels
{
    /// <summary>
    /// View Model Page Pictures
    /// </summary>
    public class PagePicturesViewModel : PageViewModelBase
    {
        public override string Name
        {
            get { return "Page Pictures"; }
        }

        #region Command

        public ICommand SelectCurrentCommand
        {
            get;
            private set;
        }

        public ICommand SaveDescriptionCommand
        {
            get;
            private set;
        }

        public ICommand ItemsAddedCommand
        {
            get;
            private set;
        }

        public ICommand ItemsDeleteCommand
        {
            get;
            private set;
        }


        #endregion

        public PagePicturesViewModel(IMediaManager service)
            : base(service)
        {
            //Liste des Images
            using (new CustomWaitCursor())
            {
                Medias = new ObservableCollection<Media>(service.ListMediasByCategory(3));
                SelectCurrentCommand = new DelegatingCommand<Media>(SelectCurrent);
                SaveDescriptionCommand = new DelegatingCommand<EventParameters>(SaveDescription);
                ItemsAddedCommand = new DelegatingCommand<EventParameters>(ItemsAdded);
                ItemsDeleteCommand = new DelegatingCommand<EventParameters>(ItemsDeleted);
                mediasView = CollectionViewSource.GetDefaultView(Medias);
                mediasView.CurrentChanged += mediasView_CurrentChanged;
            }
        }

        /// <summary>
        /// Selection d'une Image
        /// </summary>
        public void SelectCurrent(Media sender)
        {
            int q = Medias.IndexOf((from c in Medias
                                    where sender.Id == c.Id
                                    select c).First());
            mediasView.MoveCurrentToPosition(q);
        }

        /// <summary>
        /// Saauvegarder la descritpion
        /// </summary>
        /// <param name="e"></param>
        public void SaveDescription(EventParameters e)
        {
            try
            {
                service.UpdateMedia(CurrentMedia);
            }
            catch (Exception ex)
            {
                if (icustomMessageBoxVisualizer != null)
                {
                    icustomMessageBoxVisualizer.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
        }

        /// <summary>
        /// Ajout d'un element
        /// </summary>
        /// <param name="e"></param>
        public void ItemsAdded(EventParameters e)
        {
            string fileName = e.CommandParameter.ToString();

            try
            {
                Media m = service.CreatePicture(fileName);
                Medias.Add(m);
            }
            catch (Exception ex)
            {
                if (icustomMessageBoxVisualizer != null)
                {
                    icustomMessageBoxVisualizer.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
        }

        /// <summary>
        /// Suppression d'un element
        /// </summary>
        /// <param name="e"></param>
        public void ItemsDeleted(EventParameters e)
        {
            try
            {
                bool continu = true;
                if (icustomMessageBoxVisualizer != null)
                {
                    continu = false;
                    if (icustomMessageBoxVisualizer.Show("Etes-vous sur de vouloir supprimer cette image ?",
                                                     "Confirmation", MessageBoxButton.YesNo,
                                                     MessageBoxImage.Question) == MessageBoxResult.Yes)
                    {
                        continu = true;
                    }
                }

                if (continu)
                {
                    service.DeletePicture(CurrentMedia);
                    Medias.Remove(CurrentMedia);
                }

            }
            catch (Exception ex)
            {
                if (icustomMessageBoxVisualizer != null)
                {
                    icustomMessageBoxVisualizer.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
        }

        /// <summary>
        /// Appelé quand le média courant à changé
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void mediasView_CurrentChanged(object sender, System.EventArgs e)
        {
            OnPropertyChanged("CurrentMedia");
        }
    }
}
